package com.gree.flowable.app.controller;

import com.gree.common.entity.ReturnVo;
import com.gree.flowable.app.mapper.FormInterfaceInfoMapper;
import com.gree.flowable.app.pojo.form.FormInterfaceInfo;
import com.gree.flowable.app.services.IFormInterfaceInfoService;
import com.gree.framework.utils.SecurityUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.security.core.context.SecurityContextHolder;
import org.springframework.web.bind.annotation.*;

import java.util.List;

//import com.gree.flowable.app.http.MyClient;

/**
 * @author ：180849
 * @date ：Created in 2021/5/24 14:44
 * @description：表单接口信息库
 * @modified By：
 * @version: 1.0
 */

@RestController
@RequestMapping("/rest/formInterface")
public class IFormInterfaceInfoApi {
    @Autowired
    private IFormInterfaceInfoService iFormInterfaceInfoService;

    @Autowired
    private FormInterfaceInfoMapper formInterfaceInfoMapper;

//    @Autowired
//    private MyClient myClient;

    /**
     * 添加回调接口信息
     * @param formInterfaceInfoList     回调接口信息
     * @return
     */
    @PostMapping("/formInterface")
    public ReturnVo addFormInterfaceInfo(@RequestBody List<FormInterfaceInfo> formInterfaceInfoList){

        return iFormInterfaceInfoService.addInfo(formInterfaceInfoList);
    }

    /**
     * 更新回调接口信息
     * @param formInterfaceInfoList     回调接口数据
     * @return
     */
    @PutMapping("formInterface")
    public ReturnVo updateFormInterfaceInfo(@RequestBody List<FormInterfaceInfo> formInterfaceInfoList){
        String systemName = SecurityContextHolder.getContext().getAuthentication().getPrincipal().toString();
        if(systemName.equals("admin")){
            systemName = null;
        }
        return iFormInterfaceInfoService.updateInterfaceInfo(formInterfaceInfoList,systemName);
    }

//    @PostMapping("/callBack/{interfaceType}")
//    public ReturnVo callBack(@PathVariable("interfaceType")String interfaceType,@RequestBody Map<String,Object> dataMap){
//        ReturnVo result = null;
//        try {
//            String clientId = SecurityUtils.getClientId();
//            String systemName = null;
//            switch (clientId){
//                case "gscm_client":
//                    systemName = null;
//                    break;
//                case "external_client":
//                    systemName = SecurityUtils.getUser().getUsername();
//                    break;
//                default:
//            }
//            QueryWrapper<FormInterfaceInfo> queryWrapper = new QueryWrapper<>();
//            queryWrapper.lambda().eq(FormInterfaceInfo::getInterfaceType,interfaceType).eq(FormInterfaceInfo::getUserName,systemName);
//            FormInterfaceInfo formInterfaceInfo = formInterfaceInfoMapper.selectOne(queryWrapper);
//            String url = formInterfaceInfo.getInterfaceAddress();
//            myClient.postCallBack(url,dataMap);
//            result = new ReturnVo(ReturnCode.SUCCESS,"数据抛出成功");
//        } catch (Exception e) {
//            result = new ReturnVo(ReturnCode.FAIL,e.getMessage());
//        }
//        return result;
//    }


}
